﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Tesla.DataAccess.DataAccessLogic.Implementations;
using Tesla.Util.Utilities;
using Tesla.Service.DataContracts;
using Tesla.DataAccess.DataAccessLogic.Interfaces;
using Tesla.Business.BusinessLogic.Interfaces;
using System.Transactions;

namespace Tesla.Business.BusinessLogic.Implementations
{
    public class ClientBL : BaseOperationalBL, IClientBL
    {

        public override int? Create(BaseDC bdc)
        {
            int? success = 0;
            try
            {
                using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 0, int.Parse(ConfigurationSettings.AppSettings["GADNCommandTimeout"]))))
                {
                    BaseOperationalDA daClient = DAFactory.CreateInstance(EntityMapper.CLIENTDA);
                    success = daClient.Create(bdc as BaseDC);
                    transactionScope.Complete();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return success;
        }

        public override int Update(BaseDC bdc)
        {
            int updateResult = 0;

            try
            {
                using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 0, int.Parse(ConfigurationSettings.AppSettings["GADNCommandTimeout"]))))
                {
                    BaseOperationalDA daClient = DAFactory.CreateInstance(EntityMapper.CLIENTDA);
                    updateResult = daClient.Update(bdc as BaseDC);
                    transactionScope.Complete();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return updateResult;
        }

        public override IList<BaseDC> DisplayAll(int gridSize, ref int? totalRecordCount)
        {
            try
            {
                BaseOperationalDA daBase = DAFactory.CreateInstance(EntityMapper.CLIENTDA);
                return daBase.DisplayAll(gridSize, ref totalRecordCount);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> SearchByName(string name, int gridSize, ref int? totalRecordCount)
        {
            try
            {
                BaseOperationalDA daBase = DAFactory.CreateInstance(EntityMapper.CLIENTDA);
                return daBase.SearchByName(name, gridSize, ref totalRecordCount);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> Read(int id)
        {

            try
            {
                BaseOperationalDA daClients = DAFactory.CreateInstance(EntityMapper.CLIENTDA);
                IList<BaseDC> clients = daClients.Read(id);

                using (ContextDA cDA = new ContextDA())
                {
                    IClientDA idaClient = cDA.CreateSpecializedDA(EntityMapper.CLIENTDA) as IClientDA;

                    if (clients != null && clients.Count > 0)
                    {
                        ((ClientDC)clients[0]).ClientParents = idaClient.ReadAllClientParents();
                        ((ClientDC)clients[0]).ClientOperatingUnits = idaClient.ReadAllOperatingUnits();
                    }
                }
                return clients;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<BaseDC> ReadProjectsForClient(int clientID)
        {
            IList<BaseDC> projects = null;

            try
            {
                using (ContextDA cDA = new ContextDA())
                {
                    IClientDA idaClient = cDA.CreateSpecializedDA(EntityMapper.CLIENTDA) as IClientDA;
                    projects = idaClient.ReadProjectsForClient(clientID);
                }
                return projects;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<BaseDC> ReadAllClientParents()
        {
            IList<BaseDC> clientParents = null;
            
            try
            {
                using (ContextDA cDA = new ContextDA())
                {
                    IClientDA idaClient = cDA.CreateSpecializedDA(EntityMapper.CLIENTDA) as IClientDA;
                    clientParents = idaClient.ReadAllClientParents();
                }
                return clientParents;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<BaseDC> ReadAllOperatingUnits()
        {
            IList<BaseDC> operatingUnits = null;

            try
            {
                using (ContextDA cDA = new ContextDA())
                {
                    IClientDA idaClient = cDA.CreateSpecializedDA(EntityMapper.CLIENTDA) as IClientDA;
                    operatingUnits = idaClient.ReadAllOperatingUnits();
                }
                return operatingUnits;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> ReadByRange(int startRecordNo, int endRecordNo, string sortColName, string sortDirection, string searchString)
        {
            try
            {
                BaseOperationalDA daBase = DAFactory.CreateInstance(EntityMapper.CLIENTDA);
                return daBase.ReadByRange(startRecordNo, endRecordNo, sortColName, sortDirection, searchString);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
